home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / src / GUSISIOUX.cp < prev    next >
Text File  |  1995-08-25  |  5KB  |  217 lines

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand unified socket interface
  3. File        :    GUSISIOUX.cp    -    Interface to Metrowerks SIOUX library
  4. Author    :    Matthias Neeracher
  5. Language    :    MPW C/C++
  6.  
  7. $Log$
  8. *********************************************************************/
  9.  
  10. #include <GUSIFile_P.h>
  11. #include <ioctl.h>
  12. #include <console.h>
  13.  
  14. #include <Events.h>
  15. #include <LowMem.h>
  16.  
  17. /************************ SIOUXSocket members ************************/
  18.  
  19. /* This declaration lies about the return type */
  20. extern "C" void SIOUXHandleOneEvent(EventRecord *userevent);
  21.  
  22. GUSIEvtHandler    GUSISIOUXEvents[]    =    {
  23.     SIOUXHandleOneEvent,        // nullEvent
  24.     
  25.     SIOUXHandleOneEvent,        // mouseDown
  26.     SIOUXHandleOneEvent,        // mouseUp
  27.     nil,                            // keyDown
  28.     nil,
  29.     
  30.     nil,                            // autoKey
  31.     SIOUXHandleOneEvent,        // updateEvt
  32.     SIOUXHandleOneEvent,        // diskEvt
  33.     SIOUXHandleOneEvent,        // activateEvt
  34.     
  35.     nil,
  36.     nil,
  37.     nil,
  38.     nil,
  39.     
  40.     nil,
  41.     nil,
  42.     SIOUXHandleOneEvent,        // osEvt
  43.     nil,
  44.     
  45.     nil,
  46.     nil,
  47.     nil,
  48.     nil,
  49.     
  50.     nil,
  51.     nil,
  52.     nil,
  53. };
  54.  
  55. /************************ Declaration of SIOUXSocket ************************/
  56.  
  57. class SIOUXSocket : public Socket    {        
  58.     friend class SIOUXSocketDomain;    
  59.     
  60.                     SIOUXSocket();
  61.                     
  62.     virtual         ~SIOUXSocket();
  63. public:
  64.     virtual int    read(void * buffer, int buflen);
  65.     virtual int write(void * buffer, int buflen);
  66.     virtual int select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  67.     virtual int    ioctl(unsigned int request, void *argp);
  68.     virtual int    isatty();
  69. };    
  70.  
  71. class SIOUXSocketDomain : public FileSocketDomain {
  72.     SIOUXSocket *    singleton;
  73. public:
  74.     SIOUXSocketDomain()    :    FileSocketDomain(AF_UNSPEC, true, false), singleton(nil)    {    }
  75.     
  76.     virtual Boolean Yours(const GUSIFileRef & ref, Request request);
  77.     virtual Socket * open(const GUSIFileRef & ref, int oflag);
  78. };
  79.  
  80. #if GENERATING68K
  81. #pragma segment SIOUX
  82. #endif
  83.  
  84. /************************ SIOUXSocket members ************************/
  85.  
  86. SIOUXSocket::SIOUXSocket()
  87. {
  88.     InstallConsole(0);
  89.     GUSISetEvents(GUSISIOUXEvents);
  90. }
  91.  
  92. SIOUXSocket::~SIOUXSocket()
  93. {
  94.     RemoveConsole();
  95. }
  96.  
  97. int SIOUXSocket::ioctl(unsigned int request, void *argp)
  98. {
  99.     switch (request)    {
  100.     case FIOINTERACTIVE:
  101.         return 0;
  102.     default:
  103.         return GUSI_error(EOPNOTSUPP);
  104.     }
  105. }
  106.  
  107. int SIOUXSocket::read(void * buffer, int buflen)
  108. {
  109.     fflush(stdout);
  110.     
  111.     return ReadCharsFromConsole((char *) buffer, buflen);
  112. }
  113.  
  114. int SIOUXSocket::write(void * buffer, int buflen)
  115. {
  116.     return WriteCharsToConsole((char *) buffer, buflen);
  117. }
  118.  
  119. static Boolean input_pending()
  120. {
  121.     QHdrPtr eventQueue = LMGetEventQueue();
  122.     EvQElPtr element = (EvQElPtr)eventQueue->qHead;
  123.     
  124.     // now, count the number of pending keyDown events.
  125.     while (element != nil) {
  126.         if (element->evtQWhat == keyDown || element->evtQWhat == autoKey)
  127.             return true;
  128.         element = (EvQElPtr)element->qLink;
  129.     }
  130.     
  131.     return false;
  132. }
  133.  
  134. int SIOUXSocket::select(Boolean * canRead, Boolean * canWrite, Boolean * exception)
  135. {
  136.     int        goodies     =     0;
  137.         
  138.     fflush(stdout);
  139.     
  140.     if (canRead) 
  141.         if (*canRead = input_pending())
  142.             ++goodies;
  143.     
  144.     if (canWrite) {
  145.         *canWrite = true;
  146.         ++goodies;
  147.     }
  148.     
  149.     if (exception)
  150.         *exception = false;
  151.     
  152.     return goodies;
  153. }
  154.  
  155. int SIOUXSocket::isatty()
  156. {
  157.     return 1;
  158. }
  159.  
  160. /********************* SIOUXSocketDomain members **********************/
  161.  
  162. extern "C" void GUSIwithSIOUXSockets()
  163. {
  164.     static SIOUXSocketDomain    SIOUXSockets;
  165.     SIOUXSockets.DontStrip();
  166. }
  167.  
  168. Boolean SIOUXSocketDomain::Yours(const GUSIFileRef & ref, FileSocketDomain::Request request)
  169. {
  170.     if (ref.spec || (request != willOpen && request != willStat))
  171.         return false;
  172.     
  173.     switch (ref.name[4] | 0x20) {
  174.     case 's':
  175.         if ((ref.name[5] | 0x20) != 't' || (ref.name[6] | 0x20) != 'd')
  176.             return false;
  177.         switch (ref.name[7] | 0x20) {
  178.         case 'i':
  179.             if ((ref.name[8] | 0x20) != 'n' || ref.name[9])
  180.                 return false;
  181.             return true;
  182.         case 'o':
  183.             if ((ref.name[8] | 0x20) != 'u' || (ref.name[9] | 0x20) != 't' || ref.name[10])
  184.                 return false;
  185.             return true;
  186.         case 'e':
  187.             if ((ref.name[8] | 0x20) != 'r' || (ref.name[9] | 0x20) != 'r' || ref.name[10])
  188.                 return false;
  189.             return true;
  190.         default:
  191.             return false;
  192.         }
  193.     case 'c':
  194.         if (    (ref.name[5] | 0x20) != 'o' || (ref.name[6] | 0x20) != 'n'
  195.             || (ref.name[7] | 0x20) != 's' || (ref.name[8] | 0x20) != 'o'
  196.             || (ref.name[9] | 0x20) != 'l' || (ref.name[10] | 0x20) != 'e')
  197.             return false;
  198.         switch (ref.name[11]) {
  199.         case 0:
  200.             return true;
  201.         default:
  202.             return false;
  203.         }
  204.     default:
  205.         return false;
  206.     }
  207. }
  208.  
  209. Socket * SIOUXSocketDomain::open(const GUSIFileRef & ref, int flags)
  210. {
  211.     if (!singleton)
  212.         singleton = new SIOUXSocket();
  213.     ++*singleton;
  214.     
  215.     return singleton;
  216. }
  217.